package com.study.jyg.rxjava2study.adapter;

import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.activity.GameActivity;
import com.study.jyg.rxjava2study.finaly.AnimationListener;
import com.study.jyg.rxjava2study.utils.TimerUtils;

import java.util.HashMap;
import java.util.Map;

public class MouseAdapter extends BaseQuickAdapter<GameActivity.MouseBean, BaseViewHolder> {

    private Map<Integer, CountDownTimer> map;

    private CountDownTimer outTimer;//露头
    private CountDownTimer clickTimer;//挨打
    private CountDownTimer intTimer;//缩头

    public MouseAdapter() {
        super(R.layout.item_game);
        map = new HashMap<>();
    }

    private void startOutTimer() {
//        outTimer =
    }

    @Override
    protected void convert(BaseViewHolder helper, GameActivity.MouseBean item) {
        ImageView ivMouse = helper.getView(R.id.ivMouse);
        switch (item.getType()) {
            case 0:
                Log.e("case", "case=0 positon=" + helper.getAdapterPosition());
                ivMouse.setBackgroundResource(R.color.colorLucency);
                break;
            case 1://冒头
                Log.e("case", "case=1 positon=" + helper.getAdapterPosition());
                CountDownTimer countDownTimer = map.get(helper.getAdapterPosition());
                if (countDownTimer != null) countDownTimer.cancel();
                countDownTimer = null;
                map.put(helper.getAdapterPosition(), TimerUtils.countDown(2000, 1000, new TimerUtils.CountDownTimerListener() {
                    @Override
                    public void onTick(long l) {

                    }

                    @Override
                    public void onFinish() {
                        Log.e("case", "case=1 positon=" + helper.getAdapterPosition() + " countFinifsh");
                        animClose(ivMouse, new AnimationListener() {
                            @Override
                            public void onAnimationEnd(Animation animation) {
                                Log.e("case", "case=1 positon=" + helper.getAdapterPosition() + " onAnimationEnd");
                                ivMouse.setBackgroundResource(R.color.colorLucency);
                                item.setType(0);
                                notifyItemChanged(helper.getAdapterPosition());

                            }
                        });
                    }
                }));
                animOpen(ivMouse);
                ivMouse.setBackgroundResource(R.mipmap.icon_mouse);
                break;
            case 2://挨打
                Log.e("case", "case=2 positon=" + helper.getAdapterPosition());
                ivMouse.setBackgroundResource(R.mipmap.icon_mouse_press);
                CountDownTimer timer = map.get(helper.getAdapterPosition());
                if (timer != null) timer.cancel();
                new Handler().postDelayed(() -> {
                    Log.e("case", "case=2 positon=" + helper.getAdapterPosition() + " postDelayed");
                    animClose(ivMouse, new AnimationListener() {
                        @Override
                        public void onAnimationEnd(Animation animation) {
                            Log.e("case", "case=2 positon=" + helper.getAdapterPosition() + " onAnimationEnd");
                            ivMouse.setBackgroundResource(R.color.colorLucency);
                            item.setType(0);
                            notifyItemChanged(helper.getAdapterPosition());
                        }
                    });
                }, 1000);
                break;
        }
    }

    /**
     * 展开动画
     */
    private void animOpen(ImageView ivMouse) {
        ivMouse.setVisibility(View.VISIBLE);
        Animation animationOpen = AnimationUtils.loadAnimation(getContext(), R.anim.anim_dis_unfold);
        animationOpen.setDuration(300);
        animationOpen.setFillAfter(true);
        ivMouse.startAnimation(animationOpen);
    }

    /**
     * 关闭动画
     */
    private void animClose(ImageView ivMouse, AnimationListener listener) {
        Animation animationClose = AnimationUtils.loadAnimation(getContext(), R.anim.anim_dis_close);
        animationClose.setDuration(300);
        animationClose.setFillAfter(true);
        animationClose.setAnimationListener(listener);
        ivMouse.startAnimation(animationClose);
    }
}
